package com.gj.subclass.comtroller;

import com.gj.subclass.enumpackge.StatusEnum2;
import com.gj.subclass.model.*;
import com.gj.subclass.service.SubClassService;
import com.gj.utli.AllRecords;
import com.gj.utli.ResultBuilderVo;
import com.gj.utli.ResultVo;
import com.sun.org.apache.regexp.internal.RE;
import org.apache.tomcat.util.net.SSLUtilBase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Tangyunhen on 2021-06-24.
 */
@RestController
//@RequestMapping("subclass")
@CrossOrigin //支持跨域
public class SubClassController {

    @Autowired
    private SubClassService subClassService;
    /**
     * 分页查询接口
     */
    @PostMapping("subClass/queryPage")
    public ResultVo queryPage(@RequestBody SubClassSelectVo subClassSelectVo){
       return subClassService.queryPage(subClassSelectVo);
    }

    @PostMapping("subClass/add")
    public ResultVo add(@RequestBody @Valid SubClassAddVo subClassAddVo, BindingResult bindingResult){
      if (bindingResult.hasErrors()){
          return ResultBuilderVo.error(bindingResult.getFieldError().getDefaultMessage());
      }
         return subClassService.add(subClassAddVo);
    }
    //修改
    @PostMapping("subClass/update")
    public ResultVo add(@RequestBody @Valid SubClassUpdateVo subClassUpdateVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuilderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return subClassService.update(subClassUpdateVo);
    }
    @PostMapping("subClass/updateStatus")
    public ResultVo add(@RequestBody @Valid SubClassUpdateStatusVo subClassUpdateStatusVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuilderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return subClassService.updatestatus(subClassUpdateStatusVo);
    }

    //查看
    @GetMapping("see")
    public ResultVo see(@RequestParam("id")Integer id){
        if (id==null){
            return ResultBuilderVo.error("id不能为空");
        }
           return subClassService.see(id);
    }


    @PostMapping("/metadata/getPullDownList")
    public ResultVo xiala(){
        List<StatusVo> statusVos=new ArrayList<>();

        for (StatusEnum2 c: StatusEnum2.values()){
            statusVos.add(new StatusVo(c.getStatusName(),c.getStatus()));
    }
        return ResultBuilderVo.success(statusVos);
    }
}
